﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using WWNotice.constant;
using WWNotice.Utils;

namespace WWNotice.service
{
    /// <summary>
    /// 访问数据回调后的处理
    /// </summary>
    public class DataHandle
    {
        private WebBrowser webBrowser;
        private TextBox textBox;


        public DataHandle(WebBrowser webBrowser, TextBox textBox)
        {
            this.webBrowser = webBrowser;
            this.textBox = textBox;
        }
        public static int page = 1;
        static bool isEnd = false;
        static string error = "";

        public void init()
        {
            textBox.Text = "";
            error = "";
            page = 1;
            HttpServiceApi.serviceEnum = ServiceEnum.realTimeVisitData;
            this.textBox.Text = "实时访客：";
            // 实时数据请求
            webBrowser.Navigate(HttpServiceApi.REAL_TIME_VISIT_DATA + "1");
        }
        /// <summary>
        /// 爬取实时访客
        /// </summary>
        public void handle1()
        {
            //这个就是当网页载入完毕后要进行的操作

            string resp = webBrowser.Document.Body.OuterText;
            string pattern = "\"valueStart\":\"{.*?}\",(?=\"childFields\")";
            string pattern2 = "\"value\":\"{.*?}\",(?=\"key\")";
            Regex reg = new Regex(pattern);
            Regex reg2 = new Regex(pattern2);
            string newStr = reg.Replace(resp, "");
            string newStr2 = reg2.Replace(newStr, "");
            System.Console.WriteLine("webBrowser.Document.Body.OuterText:" + newStr2);
            if (!resp.Contains("\"list\":[]") && resp.Contains("\"code\":0") && isEnd)
            {
                string resp2 = HttpUtil.PostJsonRequest(newStr2, HttpServiceApi.REAL_TIME_VISIT_DATA_IMPORT);
                if (!resp2.Contains("\"code\":500"))
                {
                    page += 1;
                    webBrowser.Navigate(HttpServiceApi.REAL_TIME_VISIT_DATA + page);
                    this.textBox.Text += "【" + resp2 + "】 ";
                    System.Console.WriteLine(resp2);
                }
                else
                {
                    Notice.errorMessage("爬去实时访客异常:" + resp);
                    isEnd = true;
                }


            }
            else
            {
                isEnd = true;
            }

            if (isEnd)
            {
                HttpServiceApi.serviceEnum = ServiceEnum.userAssess;
                // 用户评价
                webBrowser.Navigate(HttpServiceApi.TAOBAO_USER_ASSESS);
            }

        }
        /// <summary>
        /// 用户评价接口回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void handle2()
        {
            this.textBox.Text += "\r\n用户评价管理：";
            //这个就是当网页载入完毕后要进行的操作
            HtmlDocument hdoc = webBrowser.Document;

            string resp = webBrowser.Document.Body.OuterText;
            System.Console.WriteLine(webBrowser.Document.Body.OuterText);

            //string resp2 = HttpUtil.PostJsonRequest(resp, HttpServiceApi.USER_ASSESS);
            //this.textBox.Text += "【" + resp2 + "】 ";
            //if (resp2.Contains("\"code\":500"))
            //{
            //    Notice.errorMessage("爬取用户评价异常:" + resp2);
            //}
            HttpServiceApi.serviceEnum = ServiceEnum.phoneHomeClick;
            // 首页点击分布
            string date = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            webBrowser.Navigate(string.Format(HttpServiceApi.TAOBAO_PHONE_HOME_CLICK, date, date));
        }

        /// <summary>
        /// 手淘首页点击分布
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void handle3()
        {
            this.textBox.Text += "\r\n首页分布数据：";

            string resp = webBrowser.Document.Body.OuterText;
            System.Console.WriteLine(webBrowser.Document.Body.OuterText);

            //string resp2 = HttpUtil.PostJsonRequest(resp, HttpServiceApi.PHONE_HOME_CLICK);
            //this.textBox.Text += "【" + resp2 + "】 ";
            //if (resp2.Contains("\"code\":500"))
            //{
            //    Notice.errorMessage("爬取手机首页点积分布数据异常:" + resp2);
            //}
            HttpServiceApi.serviceEnum = ServiceEnum.deliver;
            // 订单数据爬取录入
            webBrowser.Navigate(string.Format(HttpServiceApi.TAOBAO_DELIVER, DateTime.Now.AddDays(-60).ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd")), null, null, "referer:https://fahuo.cainiao.com/consigns/tb/order.htm");
        }



        /// <summary>
        /// 订单数据爬取录入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void handle4()
        {
            this.textBox.Text += "\r\n订单数据爬取录入：";

            string resp = webBrowser.Document.Body.OuterText;
            System.Console.WriteLine(webBrowser.Document.Body.OuterText);

            //string resp2 = HttpUtil.PostJsonRequest(resp, HttpServiceApi.ORDER_DETAIL);
            //this.textBox.Text += "【" + resp2 + "】 ";
            //if (!resp2.Contains("\"code\":200"))
            //{
            //    Notice.errorMessage("订单数据爬取录入异常:" + resp2);
            //}
            this.textBox.Text += "\r\n订单数据导入：";
            Dictionary<string, string> headers = new Dictionary<string, string>();
            headers["deviceKey"] = "pc-mmhf-20200109";
            string resp3 = HttpUtil.PostJsonRequest(resp, HttpServiceApi.ORDER_DATA, headers);

            this.textBox.Text += "【" + resp3 + "】 ";
            if (resp3.Contains("\"code\":412") || resp3.Contains("\"code\":500"))
            {
                Notice.errorMessage("订单数据导入异常:" + resp3);
            }

        }






    }
    /// <summary>
    /// 问大家爬取数据处理
    /// </summary>
    public class RealTimeHandle
    {
        private WebBrowser webBrowser;
        private TextBox textBox;

        public RealTimeHandle(WebBrowser webBrowser, TextBox textBox)
        {
            this.webBrowser = webBrowser;
            this.textBox = textBox;
        }

        public void handle1()
        {
            this.textBox.Text += "\r\n获取未发货数据：";

            string resp = webBrowser.Document.Body.OuterText;
            System.Console.WriteLine(webBrowser.Document.Body.OuterText);

            //string resp2 = HttpUtil.PostJsonRequest(resp, HttpServiceApi.NO_DELIVER);
            //this.textBox.Text += "【" + resp2 + "】 ";
            //if (!resp2.Contains("\"code\":200"))
            //{
            //    Notice.errorMessage("爬取未发货数据异常:" + resp2);
            //}

        }

    }

}
